package no21;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/4/23 11:50
 * 调整数组顺序使奇数位于偶数前面
 * https://leetcode.cn/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/
 */
class Solution {

    // 方法1 相对位置可以改变
    public int[] exchange(int[] nums) {
        // 使用双指针
        int left = 0;
        int right = nums.length - 1;
        while(left < right) {
            while (left < right && (nums[left] & 1) == 1) {
                left++;
            }
            while (left < right && (nums[right] & 1) != 1) {
                right--;
            }
            // 此时 left 一定指向偶数, right 一定指向奇数
            if (left < right) {
                int temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp;
            }
        }
        return nums;
    }
}
